//
//  ShoppingC.m
//  GuiJuWo
//
//  Created by chenguibang on 16/3/24.
//  Copyright © 2016年 chenguibang. All rights reserved.
//

#import "ShoppingC.h"
#import "GBWebView.h"
#import "GBWebViewVM.h"

@interface ShoppingC ()<UIWebViewDelegate,GBWebViewProgressDelegate,UISearchBarDelegate,UIScrollViewDelegate>

@end

@implementation ShoppingC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navBar.normalNavBar.titleLable.text = @"购物车";
    [RACObserve(self, baseurl) subscribeNext:^(id x) {
        [self.webViewVM loadUrlOnPost:self.baseurl];
    }];
    
    [RACObserve(self.webViewVM, requst) subscribeNext:^(id x) {
        [self.gbWebView loadRequest:self.webViewVM.requst];
    }];

    
    
    self.gbWebView.scrollView.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(GBWebView *)gbWebView{
    if (!_gbWebView) {
        _gbWebView = [[GBWebView alloc]init];;
        [self.contentView addSubview:_gbWebView];
//        _gbWebView.backgroundColor = UIColorWithRandom;
        _gbWebView.sd_layout.leftEqualToView(self.contentView).topEqualToView(self.contentView).rightEqualToView(self.contentView).bottomEqualToView(self.contentView);
        _gbWebView.delegate = self;
        _gbWebView.progressDelegate = self;
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    return _gbWebView;
}


-(UIProgressView *)progressView{
    if (!_progressView) {
        _progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar];
        [self.gbWebView addSubview:_progressView];
        _progressView.sd_layout.leftEqualToView(self.gbWebView).rightEqualToView(self.gbWebView).topEqualToView(self.gbWebView).autoHeightRatio(0);
    }
    return _progressView;
}

-(GBWebViewVM *)webViewVM{
    if (!_webViewVM) {
        _webViewVM = [[GBWebViewVM alloc]init];
    }
    return _webViewVM;
}

#pragma mark - UIWebViewDelegate
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    return YES;
}
-(void)webViewDidStartLoad:(UIWebView *)webView{
    self.progressView.hidden = NO;
    [self.progressView setProgress:0 animated:NO];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    self.progressView.hidden = YES;
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    self.progressView.hidden = YES;
}
#pragma mark -GBWebViewDelegate
-(void)webView:(GBWebView *)webView didReceiveResourceNumber:(int)resourceNumber totalResources:(int)totalResources
{
    [self.progressView setProgress:resourceNumber/totalResources animated:YES];
}

-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    self.navBar.homeSearchBar.onSearching = YES;
    return YES;
}

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    self.navBar.homeSearchBar.onSearching = NO;
}

-(void)viewTapped:(UITapGestureRecognizer*)tap1
{
    
    
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
